package cn.csg.iotplatform.module.system.service.gk;

import cn.csg.iotplatform.module.system.controller.admin.file.vo.EqtFileSaveVO;
import cn.csg.iotplatform.module.system.dal.dataobject.dvattachment.DvAttachmentDO;
import cn.csg.iotplatform.module.system.dal.dataobject.dvdeviceattachment.DvDeviceAttachmentDO;
import cn.csg.iotplatform.module.system.dal.mysql.dvattachment.DvAttachmentMapper;
import cn.csg.iotplatform.module.system.dal.mysql.dvdeviceattachment.DvDeviceAttachmentMapper;
import cn.csg.iotplatform.module.system.enums.AttachmentTypeEnum;
import cn.csg.iotplatform.module.system.service.eqfile.EqtFileService;
import cn.csg.iotplatform.module.system.service.user.AdminUserService;
import com.alibaba.excel.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Description TODO
 * @Author super huang
 * @Date 2024-05-09 11:16
 */
@Service
@Slf4j
public class GkServiceImpl implements GkService {

    @Resource
    private DvDeviceAttachmentMapper dvDeviceAttachmentMapper;

    @Resource
    private EqtFileService eqtFileService;

    @Resource
    private DvAttachmentMapper attachmentMapper;

    @Override
    public void syncAttachment() {
        List<DvAttachmentDO> attachmentDOList = attachmentMapper.getSyncAttachment();
        for(DvAttachmentDO attachmentDO : attachmentDOList) {
            try {
                if(StringUtils.isNotBlank(attachmentDO.getAttachmentFullPath())) {
                    DvDeviceAttachmentDO deviceAttachmentDO = dvDeviceAttachmentMapper.getAttachment(attachmentDO.getFlagId(), attachmentDO.getAttachmentFullPath(), attachmentDO.getJobTypeCode());
                    if(deviceAttachmentDO == null) {
                        EqtFileSaveVO eqtFileSaveVO = new EqtFileSaveVO();
                        eqtFileSaveVO.setObjectId(attachmentDO.getFlagId());
                        eqtFileSaveVO.setFlag("1");
                        eqtFileSaveVO.setJobTypeCode(attachmentDO.getJobTypeCode());
                        eqtFileSaveVO.setFileUrl(attachmentDO.getAttachmentFullPath());
                        eqtFileSaveVO.setCertificatesType(attachmentDO.getCertificatesType());
                        eqtFileSaveVO.setCertificatesLevel(attachmentDO.getCertificatesLevel());
                        eqtFileSaveVO.setCertificatesCode(attachmentDO.getCertificatesCode());
                        eqtFileService.syncFile(eqtFileSaveVO);

                        // 更新同步状态为不同步
                        attachmentMapper.updateSyncStatus(attachmentDO.getId());
                    }
                } else {
                    attachmentMapper.updateSyncStatus(attachmentDO.getId());
                }
            } catch (Exception e) {
                log.error("定时同步出差: {}", e);
            }

        }
    }
}
